package com.typing.alarm.schedule;

import java.util.Calendar;
import java.util.Timer;

import com.typing.alarm.receiver.TypingAlarmReceiver;
import com.typing.alarm.value.TypingAlarmVO;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;
import android.util.Log;

 
public class AlarmClock {

    private final Scheduler scheduler = new Scheduler();    
    private final int hourOfDay, minute, second;
    private Context mContext;        
    private Timer repeat;
    private Long delay;
    private TypingAlarmVO alarmInfo;   
    private AudioManager mAudioManager;
	private RingtoneManager ringtoneManager;
	private Ringtone alarmRingtone;
    private Vibrator vibrator;
    
	public TypingAlarmVO getAlarmInfo() {
		return alarmInfo;
	}

	public void setAlarmInfo(TypingAlarmVO alarmInfo) {
		this.alarmInfo = alarmInfo;
	}

	public AlarmClock(Context mContext,TypingAlarmVO alarmInfo, long delay) {
        this.mContext = mContext;
        String[] time = alarmInfo.getAlarmTime().split(":");       
		this.hourOfDay = Integer.parseInt(time[0]);
        this.minute = Integer.parseInt(time[1]);
        this.second = 0;  
        this.delay = delay;
        this.alarmInfo = alarmInfo;
        this.repeat = new Timer();
        this.mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
        this.vibrator = (Vibrator)mContext.getSystemService(Context.VIBRATOR_SERVICE);
    }

    public void start() {
        scheduler.schedule(new SchedulerTask() {
            public void run() {
            	Calendar cal = Calendar.getInstance();
            	boolean[] repeatDay = alarmInfo.getRepeatDay();            	
            	if(repeatDay[cal.getTime().getDay()]){            		
            		repeat.schedule(new RepeatAlarm(mContext,alarmInfo), 0 , delay);            		
            		
            		int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
                    mAudioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume, 0);
                    //mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
                    Uri uri = null;
                    if(alarmInfo.getRingSound() == null || "".equals(alarmInfo.getRingSound())){
                    	uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);                    	
                    }else{
                    	uri = Uri.parse(alarmInfo.getRingSound());                    	
                    }
                    alarmRingtone = ringtoneManager.getRingtone(mContext, uri);
                    alarmRingtone.play(); 
                    vibrator.vibrate(new long[]{100,50,200,50}, 0);
                    
            	}
            }
        }, new DailyIterator(hourOfDay, minute, second));
    }
    
    public void cancel() {
    	stopRepeat();
    	if(scheduler !=null){
    		scheduler.cancel();
    	}
    }  
    
    public void stopRepeat() {
    	if(alarmRingtone !=null){
    		alarmRingtone.stop();
    	}
    	if(repeat != null){
    		repeat.cancel();
    	}
    	if(vibrator != null){
    		vibrator.cancel();
    	}
    }
}

